/*
 * @lc app=leetcode.cn id=83 lang=typescript
 *
 * [83] 删除排序链表中的重复元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function deleteDuplicates(head: ListNode | null): ListNode | null {
    if (!head) return null;
    let newH = new ListNode();
    let now = newH;
    let cache: number | undefined = undefined;
    newH.next = now;
    while (head) {
        if (head.val !== cache) {
            const it = new ListNode(head.val);

            now.next = it;
            now = it;
            cache = head.val;
        }

        head = head.next;
    }

    return newH.next;
}
// @lc code=end
